package cn.iocoder.yudao.framework.redissonlock.config;

import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration;
import cn.iocoder.yudao.framework.redissonlock.core.aop.RedissonLockAspect;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * RedissonLock 自动配置类
 *
 * @author 徐煜东
 */
@AutoConfiguration(after = YudaoRedisAutoConfiguration.class)
public class YudaoRedissonLockConfiguration {
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.password}")
    private String password;
    @Bean
    public RedissonLockAspect redissonLockAspect(RedissonClient redissonClient,
                                                 ApplicationContext applicationContext) {
        return new RedissonLockAspect(redissonClient, applicationContext);
    }

    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password);
        return Redisson.create(config);
    }
}
